var app = getApp();
Page({

	data: {
		cdnUrl: '',
		userInfo: null,
		userEdit_type: true,
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#FFFFFF',
		show: true,
		animated: false,
		back: true,
		title: "",
    mobile: ''
	},

	onLoad: function (options) {
		var that = this;
		var type = options.type;
		if(type=="mobile"){
			that.setData({
			  title: '修改手机号码'
			});
		}else if(type=="nickname"){
			that.setData({
			  title: '姓名'
			});
		}
		that.setData({
			cdnUrl: app.cdnUrl,
			userInfo: app.globalData.userInfo,
			userEdit_type: options.type,
		});
	},

  onShow: function () {
		var that = this;
		that.setData({
			cdnUrl: app.cdnUrl
		});
    if (!app.globalData.userInfo) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }
  },

  formSubmit: function (event) {
    var that = this;
    if (event.detail.value.username == '') {
      app.error('用户名不能为空');
      return;
    }
    app.request('/addons/cms/wxapp.user/profile', event.detail.value, function (data) {
      that.setData({ userInfo: data.userInfo });
      app.globalData.userInfo = data.userInfo;
      app.success('修改成功!', function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.navigateBack({})
        }, 2000); //延迟时间
      });
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  mobileChangeSubmit: function (event) {
    var that = this;
    if (event.detail.value.captcha == '') {
      app.error('验证码不能为空');
      return;
    }
    app.request('/api/timetable.user/changemobile', event.detail.value, function (data) {
      that.setData({ userInfo: data.userInfo });
      app.globalData.userInfo = data.userInfo;
      app.success('修改成功!', function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.navigateBack({})
        }, 2000); //延迟时间
      });
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  mobileChange: function(e){
  	var value = e.detail.value;
  	this.setData({
  		mobile: value
  	})
  },
  sendCaptcha: function(){
    var that = this;
    app.request('/api/sms/send', {mobile: that.data.mobile, event: 'changemobile'}, function (data, ret) {
      app.success(ret.msg);
    }, function (data, ret) {
      app.error(ret.msg);
    });
  }

})